<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>获取地址所属街道</title>
    <style type="text/css">
        body{
            width: 960px;
            margin:0 auto;
            padding: 30px 0;
            text-align: center;
        }
        #addressList{
            width: 100%;
        }
        #search{
            width: 200px;
        }
        #resTable{
            width: 100%;
            margin-top: 20px;
            border-collapse:collapse
        }
    </style>
</head>
<body>
    <textarea id="addressList" rows="10" placeholder="请输入查询地址，多地址请用|隔开"></textarea>
    <div class="btn_wrap">
        <button id="search">查询</button>
    </div>
    <div>
        <table id="resTable" border="1">
            <tr>
                <td>原地址</td>
                <td>区县</td>
                <td>乡镇</td>
                <td>街道</td>
            </tr>
        </table>
    </div>
</body>
</html>
<script
  src="http://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
<script type="text/javascript">

    function sleep(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
            return;
        }
    }
    $("#search").on('click',function(){
        var addressList = $('#addressList').val();
        var addressArr = addressList.split("|");
        for (var i = 0;i<addressArr.length; i++) {
            (function(i) {
                setTimeout(function() {
                    var t = addressArr[i];
                    var st = getLocation(t);
                    console.log(i);
                }, (i + 1) * 300);
            })(i)
        }
    });

    function getLocation(t){
        var url = 'http://api.map.baidu.com/geocoder/v2/?address='+t+'&output=json&ak=xQev3owDfMSZCznVTO7ESO09&callback=showLocation&output=json';
        $.ajax({
            url:url,
            dataType:'jsonp',
            type:'get',
            async:false,
            success:function(data){
                var res = JSON.stringify(data);
                var jsonres = JSON.parse(res);
                var lat = jsonres.result.location.lat;
                var lng = jsonres.result.location.lng;
                var url = 'http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location='+lat+','+lng+'&output=json&pois=1&latest_admin=1&ak=xQev3owDfMSZCznVTO7ESO09&extensions_town=true';
                var street='';
                $.ajax({
                    url:url,
                    dataType:'jsonp',
                    type:'get',
                    async:false,
                    success:function(dat){
                        var res = JSON.stringify(dat);
                        var jsonres = JSON.parse(res);
                        console.log(jsonres);
                        street = jsonres.result.addressComponent.street;
                        district = jsonres.result.addressComponent.district;
                        town = jsonres.result.addressComponent.town;
                        html = '<tr><td>'+t+'</td><td>'+district+'</td><td>'+town+'</td><td>'+street+'</td></tr>';
                        $('#resTable').append(html);
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("查询失败，请查看地址输入是否有误");
                    }
                });
            },
            error:function(XMLHttpRequest, textStatus, errorThrown) {
                alert("查询失败，请查看地址输入是否有误");
            }
        });
    }

    //获取坐标
    //http://api.map.baidu.com/geocoder/v2/?address=%E5%8C%97%E4%BA%AC%E5%B8%82%E6%98%8C%E5%B9%B3%E5%8C%BA%E9%BE%99%E8%85%BE%E8%8B%91%E4%B8%89%E5%8C%BA&output=json&ak=xQev3owDfMSZCznVTO7ESO09&callback=showLocation
    
    //获取街道
    //http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=40.08169397886024,116.33752400304378&output=json&pois=1&latest_admin=1&ak=xQev3owDfMSZCznVTO7ESO09
</script>